<?php
namespace Home\Controller;

use Think\Controller;

class ThingLostController extends Controller
{
    //全部失物

    public function index()
    {
        $this->ajaxReturn($_SESSION['username']);;
    }

    public function all()
    {
        $_SESSION['href']='ThingLost/all';//记录当前的URL，实现登录后跳转到登录前界面
        $where=null;
            $time=null;
        //下面三行使得每次的分类要求都会把这三个值清零
        $_SESSION['lost_time'] = null;//标记更早以前
        $_SESSION['kind_id'] = null;
        $_SESSION['place'] = null;

        //$_SESSION['lost_or_pick'] = null;
        header("Content-type:text/html;charset=utf-8");
        //导航条的时间分类
        $today = date("Y-m-d");
        $yesterday = date("Y-m-d", strtotime("-1 day"));
        $twoDaysAgo = date("Y-m-d", strtotime("-2 day"));
        $threeDaysAgo = date("Y-m-d", strtotime("-3 day"));
        $fourDaysAgo = date("Y-m-d", strtotime("-4 day"));
        $fiveDaysAgo = date("Y-m-d", strtotime("-5 day"));
        $this->assign('today', $today);
        $this->assign('yesterday', $yesterday);
        $this->assign('twoDaysAgo', $twoDaysAgo);
        $this->assign('threeDaysAgo', $threeDaysAgo);
        $this->assign('fourDaysAgo', $fourDaysAgo);
        $this->assign('fiveDaysAgo', $fiveDaysAgo);
        //每一个失物招领启示的具体内容
        $lost_thing_record = D('kind')->getKindName();
        $this->FoundRecord = $lost_thing_record;
        //dump($lost_thing_record);
        $m = M('thing');
        //对时间分类的处理
        if (I('get.time') != null) {
            switch (I('get.time')) {
                case $today:
                    $time = $today;
                    break;
                case $yesterday:
                    $time = $yesterday;
                    break;
                case $twoDaysAgo:
                    $time = $twoDaysAgo;
                    break;
                case $threeDaysAgo:
                    $time = $threeDaysAgo;
                    break;
                case $fourDaysAgo:
                    $time = $fourDaysAgo;
                    break;
                case $fiveDaysAgo:
                    $time = $fiveDaysAgo;
                    break;
                default:
                    $time = 1;
                    break;
            }

            $where['lost_time'] = $time;
            $_SESSION['lost_time'] = $time;
        }
        //echo $where['lost_time'];
        if (I('get.lost_or_pick') != null) {
            $where["lost_or_pick"] = I('get.lost_or_pick');
            $_SESSION['lost_or_pick'] = $where['lost_or_pick'];
            //$data=$m->limit(15)->where($where)->select();
        }
        else {
            if (isset($_SESSION['lost_or_pick']))
                $where['lost_or_pick'] = $_SESSION['lost_or_pick'];
            else {
                $_SESSION['lost_or_pick'] = 0;
                $where['lost_or_pick'] = $_SESSION['lost_or_pick'];
            }
        }
        // else
        //  $data=$m->limit(15)->select();
//对物品分类的处理
        if (I('get.kind_id') != null && I('get.kind_id') != 0) {
            $where['kind_id'] = I('get.kind_id');
            $_SESSION['kind_id'] = $where['kind_id'];
            // $data = $m->where("kind_id=$id")->limit(15)->select();
        }
//对地点分类的处理
        if (I('get.place') != null) {
            $place = I('get.place');
            $where['place'] = $place;
            $_SESSION['place'] = $place;
            // $data=$m->where($where)->limit(15)->select();
        }
        //var_dump($where);
        if(isset($where))
        $data = $m->where($where)->limit(30)->order('post_time desc')->select();
        else
        $data = $m->limit(30)->order('post_time desc')->select();

        if($time!=null)
        if ($time == 1) {
            $where=null;//把where变量清空以免受到之前设置的值的影响
            if(isset($_SESSION['lost_or_pick']))
            $lost_or_pick=$_SESSION['lost_or_pick'];
            $where['lost_or_pick']=$lost_or_pick;
            $where['lost_time'] = array('not in', array($today, $yesterday, $twoDaysAgo, $threeDaysAgo, $fourDaysAgo, $fiveDaysAgo));
            $data = $m->where($where)->order('post_time desc')->limit(30)->select();
        }
        $username=$_SESSION['username'];
        $this->assign('username',$username);
        $this->assign('lost_or_pick',$_SESSION['lost_or_pick']);
        $this->assign('id', 0);
        $this->assign('uid', $_SESSION['uid']);
        $this->assign('servername',$_SERVER['SERVER_NAME']);
        //var_dump($data);
        $_SESSION['count']=0;
        //对读出来的数据进行是否过期的判断
        $m_trade=M('trade_record');
        for ($i = 0; $i < count($data); $i++) {
            $where_trade_record['thing_id'] = $data[$i]['id'];
            $trade_time = $m_trade->where($where_trade_record)->find();
            $time1 = (time() - strtotime($trade_time['trade_time'])) / 2592000;//用来记录交易时间与当前时间的时间差，若大于三个月则不显示在瀑布流界面
            if ($time1> 3 && $data[$i]['status'] == 1) {
                $data[$i]['status'] = 2;
                $status1['status'] = 2;
                $where_id['id'] = $data[$i]['id'];
                $m->where($where_id)->save($status1);
                array_splice($data,$i,1);
            }
            if($data[$i]['status']==2||$data[$i]['status']==3)
                array_splice($data,$i,1);
        }
        $this->assign('picture', $data);
        $this->display();
    }

    public function allajax()
    {
         $id = I('post.id');//记录瀑布流当前的图片块的个数
        if (isset($_SESSION['lost_or_pick']))
            $where['lost_or_pick'] = $_SESSION['lost_or_pick'];
        //$data=D('Thing')->limit($id,10)->getPicture();
        if ($_SESSION['count'] != $id) {      //这个语句用来解决滚动条滚动过快ajax重复提交多次的问题
            $m = M('thing');
            if ($_SESSION['place'] != null) {
                $where['place'] = $_SESSION['place'];
                $data = $m->where($where)->limit($id, 10)->order('post_time desc')->select();
            }
            else
            if ($_SESSION['kind_id'] != null) {
                $where=null;
                $where['lost_or_pick'] = $_SESSION['lost_or_pick'];
                $where['kind_id'] = $_SESSION['kind_id'];
                $data = $m->where($where)->limit($id, 10)->order('post_time desc')->select();
            }
            else
            if ($_SESSION['lost_time'] != null) {        //时间分类的判断
                $where = null;//清楚掉之前遗留的数据
                $where['lost_or_pick']=$_SESSION['lost_or_pick'];
                $where['lost_time'] = $_SESSION['lost-time'];
                $data = $m->where($where)->limit($id, 10)->order('post_time desc')->select();
                if ($_SESSION['lost_time'] == 1) {
                    $today = date("Y-m-d");
                    $yesterday = date("Y-m-d", strtotime("-1 day"));
                    $twoDaysAgo = date("Y-m-d", strtotime("-2 day"));
                    $threeDaysAgo = date("Y-m-d", strtotime("-3 day"));
                    $fourDaysAgo = date("Y-m-d", strtotime("-4 day"));
                    $fiveDaysAgo = date("Y-m-d", strtotime("-5 day"));
                    $where['lost_time'] = array('not in', array($today, $yesterday, $twoDaysAgo, $threeDaysAgo, $fourDaysAgo, $fiveDaysAgo));
                    $data = $m->where($where)->limit($id, 10)->order('post_time desc')->select();
                    $_SESSION['count'] = $id;
                }
            }
            else {
                if(isset($_SESSION['lost_or_pick']))
                $data = $m->where($where)->limit($id, 10)->order('post_time desc')->select();
                else
                    $data = $m->limit($id, 10)->order('post_time desc')->select();
            }
            //判断是否过期
            $m = M('trade_record');
            $m_thing = M('thing');
            for ($i = 0; $i < count($data); $i++) {
                //  $where_id=null;
                $where_trade_record['thing_id'] = $data[$i]['id'];
                $trade_time = $m->where($where_trade_record)->find();
                $time = (time() - strtotime($trade_time['trade_time'])) / 2592000;//用来记录交易时间与当前时间的时间差，若大于三个月则不显示在瀑布流界面
                if ($time > 3 && $data[$i]['status'] == 1) {
                    $data[$i]['status'] = 2;
                    $status1['status'] = 2;
                    $where_id['id'] = $data[$i]['id'];
                    $m_thing->where($where_id)->save($status1);
                }
            }
            $_SESSION['count']=$id;
            $this->ajaxReturn($data);
        }
    }
  function _empty(){
        header("HTTP/1.0 404 Not Found");//使HTTP返回404状态码
        $this->display("Error:error404");
    }
    
}